﻿<?php
//---------------------------------
// Autho: King
// Date: 2011-12-30 13:28:48
// v1.01 2012-01-06 16:38修改《凰图腾》简介无法得到
// v1.02 2012-01-09 12:39修改《京陵十三钗》VID无法得到
// v1.03 2012-02-10 10:38修改v_show时，返回的VID不正确
// http://www.youku.com/show_page/id_zcbfb06e2962411de83b1.html
// http://www.youku.com/show_page/id_z6a85e5860f0811e097c0.html
// http://v.youku.com/v_show/id_XMzMyNzU5NjAw.html
// http://www.youku.com/show_page/id_z3c315bd8c4c111e0a046.html
// http://www.youku.com/show_page/id_z965fa44c1ef411e1a046.html
// http://www.youku.com/show_page/id_z85090998867b11e0a046.html
//---------------------------------
class DetailXML
{
	private $dom;
	private $root;
	
	public function __construct($num)
	{
		$this->dom = new DOMDocument("1.0", "UTF-8"); 
		header("Content-Type: text/plain");
		
		$this->root = $this->dom->createElement("Films"); 
		$this->dom->appendChild($this->root);
		
		$Num = $this->dom->createAttribute("Num"); 
		$this->root->appendChild($Num); 
		$NumValue = $this->dom->createTextNode($num); 
		$Num->appendChild($NumValue);
	} 
	
	function addFilms($Ldesc,$Stattype,$Area,$Pubdate)
	{
		$item = $this->dom->createElement("Ldesc"); 
		$this->root->appendChild($item);
		$cdata = $this->dom->createCDATASection($Ldesc); 
		$item->appendChild($cdata); 
		
		$item = $this->dom->createElement("Stattype");
		$this->root->appendChild($item);
		$cdata = $this->dom->createCDATASection($Stattype); 
		$item->appendChild($cdata); 
		
		$item = $this->dom->createElement("Area");
		$this->root->appendChild($item);
		$cdata = $this->dom->createCDATASection($Area); 
		$item->appendChild($cdata); 
		
		$item = $this->dom->createElement("Pubdate");
		$this->root->appendChild($item);
		$cdata = $this->dom->createCDATASection($Pubdate); 
		$item->appendChild($cdata);
	} 
	
	function addFilm($DUrl)
	{
		
		$item = $this->dom->createElement("Film");
		$this->root->appendChild($item);
		
		$durl = $this->dom->createElement("DUrl"); 
		$item->appendChild($durl);
		$cdata = $this->dom->createCDATASection($DUrl); 
		$durl->appendChild($cdata); 
	}
	
	function saveDetailXML()
	{
		echo $this->dom->saveXML();
	}
}

function file_data($url) 
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
	curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
	@ $d = curl_exec($ch);
	curl_close($ch);
	
	return $d;
}

function get_v_show_detail($url)
{
	preg_match('|id_([0-9a-zA-Z=]+)\.html|', $url, $b);

	$xml = new DetailXML(1);
	$xml->addFilms(" "," "," "," ");
	$xml->addFilm($b[1]);
	$xml->saveDetailXML();
}

function get_show_page_detail($url)
{
	$Ldesc = "";$Stattype = "";$Area = "";$Pubdate = "";$num = 0;
	
	$str =  file_data($url);
	
	if(preg_match('|<span  id="show_info_short" class="short">(.*?)<\/span>|ims', $str, $b))
	{
		$Ldesc = $b[1];
	}
	else if(preg_match('|<div class="statdesc">(.*?)<\/div>|ims', $str, $tmp))
	{
		if(preg_match('|<span>(.*?)<\/span>|', $tmp[1], $b))
		{
			$Ldesc = $b[1];
		}
	}
	
	if(preg_match('|<li class="area">(.*?)<\/li>|ims', $str, $tmp))
	{
		if(preg_match('|<a target="_blank"(.*?)">(.*?)<\/a>|', $tmp[1], $b))
		{
			$Area = $b[2];
		}
	}
	
	if(preg_match('|<li class="stattype">(.*?)<\/li>|ims', $str, $tmp))
	{
		if(preg_match('|<a target="_blank"(.*?)">(.*?)<\/a>|', $tmp[1], $b))
		{
			$Stattype = $b[2];
		}
	}
	
	if(preg_match('|<li class="pub">(.*?)<\/li>|ims', $str, $tmp))
	{
		if(preg_match_all('|([0-9-]+)|', $tmp[1], $b))
		{
			$Pubdate = $b[1][1];
		}
	}
	
	//@ $str = iconv('UTF-8', 'GBK', $str);
	if(preg_match('|<span>剧集列表</span>|ims', $str))
	{
		if (preg_match('|/([0-9]+)</span>条|', $str, $b))
		{
			$pages = ceil($b[1] / 20);
		}
		else
		{
			$pages = 1;
		}
		
		if (preg_match('|id_([0-9a-zA-Z=]+)\.html|', $url, $b))
		{
			$id = $b[1];
			$content = "";
			for ($i = 1; $i <= $pages; $i++)
			{
				$u = "http://www.youku.com/show_eplist/showid_" . $id. "_type_pic_from_ajax_page_" . $i . ".html";
				$content .=  file_data($u);
			}
			
			preg_match_all('|href="http://v.youku.com/v_show/id_(.*?)\.html" target="video"></a></li>|', $content, $film);
		}
	}
	else if(preg_match_all('|<a href="http://v.youku.com/v_show/id_(.*?)\.html"  charset="(.*?) target="_blank">播放 <span class="b_r"></span></a>|ims', $str, $film))
	{
		
	}
	else if(preg_match_all('|target="_blank"  href="http://v.youku.com/v_show/id_(.*?)\.html" ><em>播放<\/em><\/a><\/div>|', $str, $film))
	{
		//print_r($film[1]);
	}
	
	$num = count($film[1]);
	//echo $Ldesc . "<br>";echo $Area . "<br>";echo $Stattype . "<br>";echo $Pubdate . "<br>";echo $num . "<br>";
	$xml = new DetailXML($num);
	$xml->addFilms($Ldesc,$Stattype,$Area,$Pubdate);
	for($i = 0; $i < $num;$i++)
	{
		$xml->addFilm($film[1][$i]);
	}
	$xml->saveDetailXML();
}

function get_youkudetail($url)
{
	$type = explode("/",$url);
	
	switch($type[3])
	{
	case "v_show":
		get_v_show_detail($url);
		break;
		
	case "show_page":
		get_show_page_detail($url);
		break;
	}
}

if (isset ($_GET['channel'])) 
{
	$url = $_GET['channel'];
	get_youkudetail($url);
}

?>